package com.yanqu.road.web.controller.usercenter.cn.activity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.impl.servercenter.CrossServerInfoDaoImpl;
import com.yanqu.road.dao.impl.servercenter.DbInfoDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.servercenter.CrossServerInfo;
import com.yanqu.road.entity.servercenter.DbInfo;
import com.yanqu.road.entity.web.cn.activity.CnActivityGetUserRankParam;
import com.yanqu.road.logic.bussiness.cn.activity.CnActivityBussiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.helper.SignHelper;
import com.yanqu.road.server.manger.AppKeyMgr;
import com.yanqu.road.utils.string.StringUtils;
import com.yanqu.road.web.mgr.AllActivityMainInfoMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
public class CnActivityGetUserRank {

    private static Logger logger = LogManager.getLogger(CnActivityGetUserRank.class.getName());

    @RequestMapping("/getCnActivityUserRank")
    public Object getCnActivityUserRank(@RequestBody CnActivityGetUserRankParam param, HttpServletRequest request) {

        JSONObject result = new JSONObject();
        result.put("state", 1);
        result.put("data", new Object());
        result.put("msg", "success");

        SortedMap<String,String> paramMap = new TreeMap<>();
        paramMap.put("serverId",String.valueOf(param.getServerId()));
        paramMap.put("type",String.valueOf(param.getType()));
        paramMap.put("activityTime",String.valueOf(param.getActivityTime()));

        if(!SignHelper.checkSign(paramMap, AppKeyMgr.getAndroidAppSecret(), param.getSign())) {
            result.put("msg", "sign校验失败");
            result.put("state", 0);
            logger.info("sign 校验失败");
            return result;
        }

        if (!eActivityType.isCnActivityType(param.getType())) {
            result.put("msg", "活动类型不支持");
            result.put("state", 0);
            logger.info("活动类型不支持");
            return result;
        }
        List<Integer> types = new ArrayList<>();
        types.add(param.getType());
        List<Integer> activityIdList = AllActivityMainInfoMgr.getActivityIdListByServerIdAndType(param.getServerId(), types, param.getActivityTime());

        // 有活动
        if (activityIdList.size() > 0) {
            ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityIdList.get(0));
            CrossServerInfo crossServerInfo = new CrossServerInfoDaoImpl().getCrossServerInfo(activityInfo.getCrossId().intValue());

            // 取得到跨服数据库
            if (crossServerInfo != null && crossServerInfo.getDbInfos().size() > 0) {
                DbInfo dbInfo = crossServerInfo.getDbInfos().get(0);
                String crossDbParam = dbInfo.getIp() + "|" +dbInfo.getPort() + "|" + dbInfo.getDbName() + "|" + dbInfo.getUserName() + "|" + dbInfo.getPassword();
                int rankConditionType = eActivityType.getCnActivityRankConditionType(param.getType());
                if (rankConditionType > 0) {
                    JSONArray jsonArray = CnActivityBussiness.getCnActivityRankList(crossDbParam, activityInfo.getActivityId(), rankConditionType);
                    if (jsonArray.size() > 0) {
                        // 获取并设置对应uid的平台ID
                        List<Long> userIdList = new ArrayList<>();
                        for (int i = 0; i < jsonArray.size(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            userIdList.add(jsonObject.getLong("userId"));
                        }

                        int serverId = Integer.valueOf(userIdList.get(0).toString().substring(0, 8));
                        DbInfo gameServerDbInfo = new DbInfoDaoImpl().getServerDbInfo(serverId).get(0);
                        String gameDbParam =  gameServerDbInfo.getIp() + "|" +gameServerDbInfo.getPort() + "|" + gameServerDbInfo.getDbName() + "|" + gameServerDbInfo.getUserName() + "|" + gameServerDbInfo.getPassword();
                        if (userIdList.size() > 0) {
                            Map<Long, String> platformUserIdMap = CnActivityBussiness.getPlatformUserIdByUserId(gameDbParam, StringUtils.listToString(userIdList, ","));
                            for (int i = 0; i < jsonArray.size(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);
                                long uid = jsonObject.getLong("userId");
                                jsonObject.put("platformUid", platformUserIdMap.get(uid));
                            }
                        }

                        result.put("data", jsonArray);
                        return result;

                    } else {
                        result.put("data", jsonArray);
                        return result;
                    }
                } else {
                    result.put("msg", "获取榜单类型异常");
                    result.put("state", 0);
                    return result;
                }

            } else {
                result.put("msg", "数据库异常");
                result.put("state", 0);

                return result;
            }
        }

        result.put("msg", "没有活动");
        result.put("state", 0);
        return result;
    }
}
